﻿Imports BUS
Imports DTO
Public Class TraCuuHocSinh

    Dim _HScrt As CHocSinhCRT

    Private Sub LoadLopHoc()
        Dim _buslop As New ClopBUS()
        cbbtenlop.DataSource = _buslop.LayDanhSachLop()
        cbbtenlop.DisplayMember = "TenLop"
        cbbtenlop.ValueMember = "MaLop"
    End Sub

    Private Sub CapNhatTieuChuanTraCuu(ByVal bUpdated As Boolean)
        If (bUpdated) Then
            Dim lh As CLopDTO
            Dim index As Integer = cbbtenlop.SelectedIndex
            lh = cbbtenlop.Items(index)
            _HScrt.HoTen = txtten.Text
            _HScrt.NgaySinhtu = dtpngaysinhtu.Value.Date
            _HScrt.NgaySinhden = dtpngaysinhden.Value.Date
            _HScrt.DiaChi = txtdiachi.Text
            _HScrt.MaLop = lh.MaLop
            _HScrt.CheckNgaySinh = cbngaysinh.Checked
            _HScrt.CheckDiaChi = cbdiachi.Checked
            _HScrt.CheckLopHoc = cbtenlop.Checked
        Else
            txtten.Text = _HScrt.HoTen
            dtpngaysinhtu.Value = _HScrt.NgaySinhtu.Date
            dtpngaysinhden.Value = _HScrt.NgaySinhden.Date
            txtdiachi.Text = _HScrt.DiaChi
            cbngaysinh.Checked = _HScrt.CheckNgaySinh
            cbdiachi.Checked = _HScrt.CheckDiaChi
            cbtenlop.Checked = _HScrt.CheckLopHoc
        End If
    End Sub

    Private Sub btntracuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntracuu.Click
        Try
            dtgvdanhsachhs.Rows.Clear()
            CapNhatTieuChuanTraCuu(True)
            Dim _Bus As New CHocSinhBus()
            Dim _danhsachhs As New List(Of CHocSinhCRT)
            _danhsachhs = _Bus.TraCuuHocSinh(_HScrt)
            Dim i As Integer
            For i = 0 To _danhsachhs.Count - 1
                dtgvdanhsachhs.Rows.Add(i + 1, _danhsachhs(i).HoTen, _danhsachhs(i).TenLop, _danhsachhs(i).TBHKI, _danhsachhs(i).TBHKII)
            Next

        Catch ex As Exception
            MessageBox.Show("Loi")
        End Try
        
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub


    Private Sub cbngaysinhtu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbngaysinh.CheckedChanged
        If (cbngaysinh.Checked) Then
            dtpngaysinhtu.Enabled = True
            dtpngaysinhden.Enabled = True
        Else
            dtpngaysinhtu.Enabled = False
            dtpngaysinhden.Enabled = False
        End If
    End Sub

    Private Sub cbdiachi_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbdiachi.CheckedChanged
        If (cbdiachi.Checked) Then
            txtdiachi.Enabled = True
        Else
            txtdiachi.Enabled = False
        End If
    End Sub

    Private Sub cbtenlop_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbtenlop.CheckedChanged
        If (cbtenlop.Checked) Then
            cbbtenlop.Enabled = True
        Else
            cbbtenlop.Enabled = False
        End If
    End Sub

    Private Sub TraCuuHocSinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadLopHoc()
            _HScrt = New CHocSinhCRT()
            CapNhatTieuChuanTraCuu(False)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class